﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TemplateCategory.aspx.cs" Inherits="CRMWeb.Pages.Admin.TemplateCategory" %>

<!DOCTYPE html>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Category</title>
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
</head>
<body>
    <form id="frmSMSCategory" method="post">
        <div id="pnlCategory" title=" SMS Category" class="easyui-panel" style="width: auto; height: 300px">
            <div class="fitem" style="text-align: left">
                <a id="lnknewCategory" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="NewCategory()">New </a>&nbsp;
                <a id="lnkdeleteCategory" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteCategory()">Delete </a>
            </div>
            <br />
            <div class="fitem">
                <input id="hdnCategoryid" type="hidden" />
                <label id="lblCategory" style="width: 150px; margin-left: 10px"></label>
                <span id="spnNewCategory">
                    <select class="easyui-combotree" id="cmbCategory" style="width: 400px;"
                        data-options="url:'/WebServices/Category.ashx?method=DisplayCategoryList&CategoryType='+ getQuerystring('type', '') ,panelHeight:'150',required:true, onSelect:selectCategory">
                    </select>
                </span>
                <input id="txtNewCategory" class="easyui-validatebox" required="true" style="display: none" />

            </div>

            <div class="fitem">
                <label style="width: 150px; margin-left: 10px">Parent Category :</label>
                <select class="easyui-combotree" id="cmbParentCategory" style="width: 400px;"
                    data-options="url:'/WebServices/Category.ashx?method=DisplayCategoryList&CategoryType=' + getQuerystring('type', ''),panelHeight:'150',required:true ">
                </select>

            </div>
            <input id="txtOprSMS" class="easyui-validatebox" required="true" hidden="true" />

            <br />
            <br />
            <div style="text-align: right; vertical-align: middle; width: auto; height: 30px; margin-right: 10%">
                <a id="lnkCancle" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="cancelOperation()">Cancel</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveCategory()">Save </a>
            </div>
        </div>


    </form>
</body>
<script type="text/javascript">

    function getQuerystring(key, defaultValue) {
        if (defaultValue == null) defaultValue = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return defaultValue;
        else {
            return qs[1];
        }
    }
    $(document).ready(function () {
        $('#pnlCategory').panel('setTitle', getQuerystring('type', '') + ' Category');
        $('#lblCategory').html('Select ' + getQuerystring('type', '') + ' Category :');
        $('#cmbParentCategory').combobox('textbox').bind('click', function () {
            $('#cmbParentCategory').combobox('showPanel');
        });

        $('#cmbCategory').combobox('textbox').bind('click', function () {
            $('#cmbCategory').combobox('showPanel');
        });
        $('#lnkCancle').hide();
        $('#lnkdeleteCategory').hide();
        $('#hdnCategoryid').val(0);
    });
    function selectCategory(node) {
        $('#hdnCategoryid').val(node.id);
        $.ajax({
            url: '/WebServices/Category.ashx?method=SelectParentCategory',
            data: { category_id: node.id, CategoryType: getQuerystring('type', '') },
            success: function (data) {
                $('#cmbParentCategory').combotree('setValue', data);

            }
        });

        $('#lnkdeleteCategory').show();
    }

    function NewCategory() {
        $('#hdnCategoryid').val(0);
        $('#pnlCategory').panel('setTitle', 'Create New ' + getQuerystring('type', '') + ' Category');
        $('#lblCategory').html(' New ' + getQuerystring('type', '') + ' Category :');
        $('#txtNewCategory').val('');
        $('#cmbParentCategory').combotree('reload');
        $('#cmbCategory').combotree('reload');
        $('#cmbCategory').combotree('setValue', '');
        $('#cmbParentCategory').combotree('setValue', '');

        $('#txtNewCategory').show();
        $('#spnNewCategory').hide();

        $('#lnkdeleteCategory').hide();
        $('#lnkCancle').show();
        $('#lnknewCategory').hide();
    }
    function cancelOperation() {
        $('#hdnCategoryid').val(0);
        $('#pnlCategory').panel('setTitle', getQuerystring('type', '') + ' Category ');
        $('#lblCategory').html('Select ' + getQuerystring('type', '') + ' Category :');
        $('#txtNewCategory').val('');
        $('#cmbParentCategory').combotree('reload');
        $('#cmbCategory').combotree('reload');
        $('#cmbCategory').combotree('setValue', '');
        $('#cmbParentCategory').combotree('setValue', '');

        $('#txtNewCategory').hide();
        $('#spnNewCategory').show();

        $('#lnkdeleteCategory').hide();
        $('#lnkCancle').hide();
        $('#lnknewCategory').show();
    }


    function deleteCategory() {
        $.ajax({
            url: '/WebServices/Category.ashx?method=deleteCategory',
            data: {
                CategoryType: getQuerystring('type', ''),
                CategoryID: $('#hdnCategoryid').val()
            },
            success: function (data) {
                if (data == 1) {
                    $.messager.alert('Morgan CRM', getQuerystring('type', '') + 'Template Category deleted successfully');
                    cancelOperation();
                }
                else if (data == -1)
                    $.messager.alert('Morgan CRM', ' This category has one or more Template !');
                else if (data == -2)
                    $.messager.alert('Morgan CRM', ' Parent Category can not be deleted !');
                else
                    $.messager.alert('Morgan CRM', ' Operation Failed !');
            }
        });

    }
    function saveCategory() {
        var tr = $('#cmbParentCategory').combotree('tree');	// get the tree object
        var parent = tr.tree('getSelected');
        var Category_name = '';

        var tr = $('#cmbCategory').combotree('tree');	// get the tree object
        var category = tr.tree('getSelected');

        if ($('#hdnCategoryid').val() == 0)
            Category_name = $('#txtNewCategory').val();
        else
            Category_name = category.text;

        $.ajax({
            url: '/WebServices/Category.ashx?method=saveCategory',
            data: {
                CategoryType: getQuerystring('type', ''),
                CategoryID: $('#hdnCategoryid').val(),
                CategoryName: Category_name,
                ParentId: parent.id
            },
            success: function (data) {
                if (data == 1) {
                    if ($('#hdnCategoryid').val() == 0)
                        $.messager.alert('Morgan CRM', ' New ' + getQuerystring('type', '') + 'Template Category saved successfully');
                    else
                        $.messager.alert('Morgan CRM', getQuerystring('type', '') + ' Template Category updated successfully');

                    cancelOperation();
                }
                else
                    $.messager.alert('Morgan CRM', 'Operation Failed !');

            }
        });
     
    }
</script>
</html>
